<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="/static/layui/css/layui.css">
    <script src="/static/jquery-2.1.4.js" type="text/javascript" charset="utf-8"></script>
    <script src="/static/layui/layui.js" type="text/javascript" charset="utf-8"></script>
    <script src="/static/mylayer.js" type="text/javascript" charset="utf-8"></script>
    <script src="/static/kindeditor/kindeditor.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<form class="layui-form" action="" lay-filter="updateFormFilter">
    <input type="hidden" name="id">
    <div class="layui-form-item">
        <label class="layui-form-label">书名</label>
        <div class="layui-input-block">
            <select id="bookId" name="bookId" lay-filter="bookId";>
                <option value="">请选择</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">借阅人名称</label>
        <div class="layui-input-block">
            <select id="cardId" name="cardId" lay-filter="cardId";>
                <option value="">请选择</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">借阅天数</label>
        <div class="layui-input-block">
            <input type="text" name="day" lay-verify="required" placeholder="请输入" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="submit">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
</form>

<script>
    layui.use(['form'], function () {
        var form = layui.form;

        $.post(
            '/book?method=selectAll',
            function (result) {
                if (result.code == 0) {
                    $(result.data).each(function () {
                        $('#bookId').append('<option value="'+this.id+'">'+this.name+'</option>');
                    })
                    form.render('select');
                }
            },
            'json'
        );

        $.post(
            '/card?method=selectAll',
            function (result) {
                if (result.code == 0) {
                    $(result.data).each(function () {
                        $('#cardId').append('<option value="'+this.id+'">'+this.name+'</option>');
                    })
                    form.render('select');
                }
            },
            'json'
        );

        var kindEditorParams = {
            afterBlur: function() {
                this.sync();
            }
        };
        var editor = KindEditor.create('#contentId', kindEditorParams);


        // 绑定提交事件
        form.on('submit(submit)', function (data) {
            var field = data.field; // 获取表单字段值
            console.log(data);
            // 此处可执行 Ajax 等操作
            $.post(
                '/record?method=add',
                field,
                function (result) {
                    console.log(result);
                    if (result.code == 0) {
                        mylayer.okMsg(result.msg);
                        setInterval(function() {
                            //关闭弹出框
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            //刷新父页面
                            window.parent.location.reload();
                        }, 2000)
                    } else {
                        mylayer.errorMsg(result.msg);
                    }
                },
                'json'
            );
            return false; // 阻止默认 form 跳转

        });
    });
</script>
</body>
</html>